package com.chuanjiabao.wisdom.ui;

import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.application.FApplication;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.umeng.message.PushAgent;

import me.leolin.shortcutbadger.ShortcutBadger;

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 锁定竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.F2C3749));
        }

        ActivityStack.getScreenManager().pushActivity(this);
        PushAgent.getInstance(this).onAppStart();
    }

    //设置标题栏中心标题
    public void setTitleText(String str) {
        TextView text = findViewById(R.id.bar_title);
        if (text != null) text.setText(str);
    }

    //设置标题栏右侧字体（如果是文字）
    public void setRightText(String str) {
        TextView text = findViewById(R.id.rightText);
        if (text != null) {
            text.setText(str);
            text.setOnClickListener(this);
        }
    }

    //设置标题栏右侧字体颜色（如果是文字）
    public void setRightTextColor(int id) {
        TextView text = findViewById(R.id.rightText);
        if (text != null) {
            text.setTextColor(this.getResources().getColor(id));
        }
    }

    public void setRightTextDraw(int draw) {
        TextView text = findViewById(R.id.rightText);
        if (text != null) {
            text.setBackgroundResource(draw);
        }
    }

    //设置标题栏右侧图片
    public void setRightImg(int id) {
        ImageView image = findViewById(R.id.rightImage);
        if (image != null) {
            image.setImageResource(id);
            image.setOnClickListener(this);
        }
    }

    //设置标题栏左侧字体（如果是文字）
    public void setLeftText(String str) {
        findViewById(R.id.topBarRelative).setVisibility(View.GONE);
        findViewById(R.id.leftTitleText).setVisibility(View.VISIBLE);
        TextView text = findViewById(R.id.leftTitleText);
        text.setText(str);
        text.setOnClickListener(this);
    }


    public void setTitleClick() {
        findViewById(R.id.bar_title).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) { }

    //标题栏返回调用方法
    public void setBack() {
        findViewById(R.id.topBarRelative).setVisibility(View.VISIBLE);
        findViewById(R.id.leftTitleText).setVisibility(View.GONE);
        findViewById(R.id.topBarRelative).setOnClickListener(view -> finish());
    }

    private ProgressDialog mProgressDialog;

    public void showProgressDialog() {
        this.showProgressDialog("数据获取中", true);
    }

    public void showProgressDialog(String msg, boolean cancelable) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setCancelable(cancelable);
        }
        if (TextUtils.isEmpty(msg)) {
            msg = "正在加载数据...";
        }
        mProgressDialog.setMessage(msg);
        mProgressDialog.show();
    }

    /**
     * 取消加载dialog
     */
    public void cancelProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.cancel();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        FApplication.badgeNumber = 0;
        String osName = StringUtil.getOSName();
        switch (osName) {
            case "huawei":
                ShortcutBadger.applyCount(this, 0);
                break;
            default:
                ShortcutBadger.removeCount(this);
                break;
        }
    }
}
